﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_SYSTEM;
using ERP_RORZE_TOOL.Tool.Forms;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_TOOL.Tool.UserControls
{
    public partial class ucBorrow : DevExpress.XtraEditors.XtraUserControl
    {
        private int index = 0;
        private bool splitReturn = false;
        private bool splitBroken = false;

        public int _action = 1;
        BorrowHelper helper = new BorrowHelper(new Borrow());
        DataTable dt = new DataTable();
        public GridView GridView;
        DXPopupMenu menu;
        private bool isDoubleClick =true;
        private LogUtils log;

        #region Handle Methods

        public ucBorrow()
        {

            InitializeComponent();
            ShowHideLink();
            InitDateRangeMenu();
            BindDatatoGrid();
            gridView1.BestFitColumns();

            splitContainerChild.PanelVisibility = SplitPanelVisibility.Panel1;
            splitContainerParent.PanelVisibility = SplitPanelVisibility.Panel1;

            this.dropbtDateRange.DropDownControl = menu;
            this.dropbtDateRange.Text = ToolConstants.Configuration.DateRangeTitle.SixMonths;
            log = new LogUtils();

        }

        private void btAddNew_Click_1(object sender, EventArgs e)
        {
            AddNew();
        }

        private void btEdit_Click_1(object sender, EventArgs e)
        {
            EditRow();
        }

        private void btDelete_Click_1(object sender, EventArgs e)
        {
            DeleteRows();
        }

        private void gridView1_CustomDrawEmptyForeground_1(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
            if (gridView1.RowCount > 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }

        private void gridView1_DoubleClick_1(object sender, EventArgs e)
        {
            var gridView = (GridView)sender;
            var point = gridView.GridControl.PointToClient(Control.MousePosition);

            DoDoubleClick(gridView, point);
        }

        private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNew();
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditRow();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DeleteRows();
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            BindDatatoGrid();
            BindDatatoReturn();
            BindDatatoBroken();
            Cursor.Current = Cursors.Default;
        }

        private void returnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucReturn ucReturn = new ucReturn();
            frm_ReturnAdd fr = new frm_ReturnAdd(ucReturn, this, true);
            var row = new DataRow[gridView1.SelectedRowsCount];

            row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
            string toolId = row[0][ToolConstants.Entities.Return.FieldName.ToolID].ToString();
            string machineId = row[0][ToolConstants.Entities.Return.FieldName.MachineID].ToString();
            int number = CheckInputReturn(toolId, machineId);

            fr.FromBorrow = true;
            fr.ToolId = toolId;
            fr.MachineId = machineId;

            if (number <= 0)
            {
                MessageBox.Show(string.Format(ToolConstants.Message.Return.MachineNotBorrow, machineId, toolId), ToolConstants.Form.MessageBox.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                MessageBox.Show(string.Format(ToolConstants.Message.Return.MachineBorrow, machineId, number.ToString(), toolId), ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK, MessageBoxIcon.Information);
                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Show();
                fr.Activate();
            }

            Cursor.Current = Cursors.Default;
        }

        private void brokenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucBroken ucBroken = new ucBroken();
            frm_BrokenAdd fr = new frm_BrokenAdd(ucBroken, this, true);
            var row = new DataRow[gridView1.SelectedRowsCount];

            row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
            string toolId = row[0][ToolConstants.Entities.Broken.FieldName.ToolID].ToString();
            string machineId = row[0][ToolConstants.Entities.Broken.FieldName.MachineID].ToString();
            int number = CheckInputReturn(toolId, machineId);

            fr.FromBorrow = true;
            fr.ToolId = toolId;
            fr.MachineId = machineId;

            if (number <= 0)
            {
                MessageBox.Show(string.Format(ToolConstants.Message.Broken.MachineNotBorrow, machineId, toolId), ToolConstants.Form.MessageBox.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                MessageBox.Show(string.Format(ToolConstants.Message.Broken.MachineBorrow, machineId, toolId, number.ToString()), ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK, MessageBoxIcon.Information);
                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Show();
                fr.Activate();
            }

            Cursor.Current = Cursors.Default;
        }

        private void gridControl1_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DeleteRows();
            }
        }

        private void showReturnPanelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (splitReturn)
            {
                showReturnPanelToolStripMenuItem.CheckState = CheckState.Unchecked;
                checkboxReturn.CheckState = CheckState.Unchecked;
                splitReturn = false;
                ShowHideReturnPanel(splitReturn);
            }
            else
            {
                showReturnPanelToolStripMenuItem.CheckState = CheckState.Checked;
                checkboxReturn.CheckState = CheckState.Checked;
                splitReturn = true;
                ShowHideReturnPanel(splitReturn);
            }
        }

        private void showBrokenPanelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (splitBroken)
            {
                showBrokenPanelToolStripMenuItem.CheckState = CheckState.Unchecked;
                checkboxBroken.CheckState = CheckState.Unchecked;
                splitBroken = false;
                ShowHideBrokenPanel(splitBroken);
            }
            else
            {
                showBrokenPanelToolStripMenuItem.CheckState = CheckState.Checked;
                checkboxBroken.CheckState = CheckState.Checked;
                splitBroken = true;
                ShowHideBrokenPanel(splitBroken);
            }
        }

        private void gridViewBroken_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
            if (gridViewBroken.RowCount > 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }

        private void gridViewReturn_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
            if (gridViewReturn.RowCount > 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.DataRowCount > 0 && (splitBroken || splitReturn))
            {
                try
                {
                    Cursor.Current = Cursors.WaitCursor;
                    var row = gridView1.GetFocusedDataRow();
                    string toolId = row[ToolConstants.Entities.Borrow.FieldName.ToolID].ToString();
                    string machineId = row[ToolConstants.Entities.Borrow.FieldName.MachineID].ToString();

                    if (splitReturn)
                    {

                        gridViewReturn.ActiveFilterString = "[ToolID] = '" + toolId + "' AND [MachineID] = '" + machineId + "'";
                    }

                    if (splitBroken)
                    {
                        gridViewBroken.ActiveFilterString = "[ToolID] = '" + toolId + "' AND [MachineID] = '" + machineId + "'";

                    }

                    Cursor.Current = Cursors.Default;
                }
                catch (Exception)
                {
                }

            }
        }

        private void checkboxReturn_CheckedChanged(object sender, EventArgs e)
        {
            if (splitReturn)
            {
                showReturnPanelToolStripMenuItem.CheckState = CheckState.Unchecked;
                splitReturn = false;
                ShowHideReturnPanel(splitReturn);
            }
            else
            {
                showReturnPanelToolStripMenuItem.CheckState = CheckState.Checked;
                splitReturn = true;
                ShowHideReturnPanel(splitReturn);
            }
        }

        private void checkboxBroken_CheckedChanged(object sender, EventArgs e)
        {
            if (splitBroken)
            {
                showBrokenPanelToolStripMenuItem.CheckState = CheckState.Unchecked;
                splitBroken = false;
                ShowHideBrokenPanel(splitBroken);
            }
            else
            {
                showBrokenPanelToolStripMenuItem.CheckState = CheckState.Checked;
                splitBroken = true;
                ShowHideBrokenPanel(splitBroken);
            }
        }
        #endregion

        public void BindDatatoGrid(int range = 1)
        {
            dt = helper.GetAll(range);

            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
            this.GridView = gridView1;
            Utilities.GridUtil.SelectRow(gridView1, index, _action);
        }

        public void BindDatatoBroken()
        {
            BrokenHelper helper = new BrokenHelper(new Broken());
            gridBroken.DataSource = helper.GetAll(0);
            gridViewBroken.BestFitColumns();
        }
        
        public void BindDatatoReturn()
        {
            ReturnHelper helper = new ReturnHelper(new Return());
            gridReturn.DataSource = helper.GetAll(0);
            gridViewReturn.BestFitColumns();
        }

        private void DoDoubleClick(GridView v, Point p)
        {
            if (!isDoubleClick) return;

            Cursor.Current = Cursors.WaitCursor;
            _action = 1;
            index = gridView1.FocusedRowHandle;
            var info = v.CalcHitInfo(p);
            if (info.InRow || info.InRowCell)
            {

                var row = new DataRow[gridView1.SelectedRowsCount];
                row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
                int iD = int.Parse(row[0][ToolConstants.Entities.Borrow.FieldName.ID].ToString());

                helper.ValueObject.ID = iD;
                helper.LoadOne();

                frm_BorrowEdit fr = new frm_BorrowEdit(this);
                fr.Id = iD;
                fr.Date = helper.ValueObject.Date;
                fr.ToolId = helper.ValueObject.ToolID;
                fr.Staff = helper.ValueObject.StaffID;
                fr.Machine = helper.ValueObject.MachineID;
                fr.Quantity = helper.ValueObject.Quantity;
                fr.Note = helper.ValueObject.Note;

                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Show();
                fr.Activate();
            }
            Cursor.Current = Cursors.Default;
        }
        private void DeleteRows()
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                Cursor.Current = Cursors.WaitCursor;
                var row = new DataRow[gridView1.SelectedRowsCount];
                var BorrowId = new int[gridView1.SelectedRowsCount];

                for (int i = 0; i < gridView1.SelectedRowsCount; i++)
                {
                    row[i] = gridView1.GetDataRow(gridView1.GetSelectedRows()[i]);
                    BorrowId[i] = int.Parse(row[i][ToolConstants.Entities.Borrow.FieldName.ID].ToString());
                }


                if (MessageBox.Show(ToolConstants.Message.DeleteQuestion, ToolConstants.Form.MessageBox.Warning,
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    BorrowHelper helper = new BorrowHelper(new Borrow());
                    for (int i = 0; i < BorrowId.Length; i++)
                    {
                        helper.ValueObject.ID = BorrowId[i];
                        if (helper.Delete() > 0)
                        {

                            //write Log
                            log.Table = LogConstant.Table.ToolCutting.Borrow;
                            log.Task = LogConstant.Task.Delete;
                            log.Description = "TooID: "+row[i][ToolConstants.Entities.Borrow.FieldName.ToolID].ToString();
                            log.WriteLog();

                            _action = 2;
                        }
                        else
                        {
                            MessageBox.Show(ToolConstants.Message.DeleteFailed, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                    }

                    BindDatatoGrid();

                }
                Cursor.Current = Cursors.Default;
            }
            else
            {
                MessageBox.Show(ToolConstants.Message.TableIsNull, ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void EditRow()
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                Cursor.Current = Cursors.WaitCursor;
                frm_BorrowEdit fr = new frm_BorrowEdit(this);
                var row = new DataRow[gridView1.SelectedRowsCount];

                index = gridView1.FocusedRowHandle;
                _action = 1;

                row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
                int iD = int.Parse(row[0][ToolConstants.Entities.Borrow.FieldName.ID].ToString());

                helper.ValueObject.ID = iD;
                helper.LoadOne();

                fr.Id = iD;
                fr.Date = helper.ValueObject.Date;
                fr.ToolId = helper.ValueObject.ToolID;
                fr.Staff = helper.ValueObject.StaffID;
                fr.Machine = helper.ValueObject.MachineID;
                fr.Quantity = helper.ValueObject.Quantity;
                fr.Note = helper.ValueObject.Note;

                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Show();
                fr.Activate();
                Cursor.Current = Cursors.Default;

            }
            else
            {
                MessageBox.Show(ToolConstants.Message.TableIsNull, ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void AddNew()
        {
            Cursor.Current = Cursors.WaitCursor;
            Form fr = new frm_BorrowAdd();
            if (!Utilities.FormUtil.IsFormOpen(fr))
            {
                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Activate();
                fr.Show();
            }
            else
            {
                Utilities.FormUtil.ActiveForm(fr);
            }

            Cursor.Current = Cursors.Default;
        }

        private int CheckInputReturn(string toolId, string machineId)
        {
            string fieldName = ToolConstants.StoreProcedure.FieldName.Total;
            string where = ToolConstants.StoreProcedure.FieldName.MachineID + "='" + machineId + "' and " + ToolConstants.StoreProcedure.FieldName.ToolID + "='" + toolId + "'";
            string storedName = ToolConstants.StoreProcedure.QTotalMachineAtFactoryBorrow;
            string tempTableName = ToolConstants.StoreProcedure.TemporaryTables.QTotalMachineAtFactoryBorrow;

            return Utilities.CheckInput(fieldName, storedName, where, true, tempTableName);

        }

        private void ShowHideReturnPanel(bool isSplitReturn)
        {
            if (isSplitReturn)
            {
                BindDatatoReturn();
                splitContainerChild.PanelVisibility = SplitPanelVisibility.Both;
            }
            else
            {
                splitContainerChild.PanelVisibility = SplitPanelVisibility.Panel1;
            }
        }

        private void ShowHideBrokenPanel(bool isSplitBroken)
        {
            if (isSplitBroken)
            {
                BindDatatoBroken();
                splitContainerParent.PanelVisibility = SplitPanelVisibility.Both;
            }
            else
            {
                splitContainerParent.PanelVisibility = SplitPanelVisibility.Panel1;
            }
        }

        private void checkboxShowFooter_CheckedChanged(object sender, EventArgs e)
        {
            if (gridView1.OptionsView.ShowFooter == true)
            {
                gridView1.OptionsView.ShowFooter = false;
                gridViewBroken.OptionsView.ShowFooter = false;
                gridViewReturn.OptionsView.ShowFooter = false;
            }
            else
            {
                gridView1.OptionsView.ShowFooter = true;
                gridViewBroken.OptionsView.ShowFooter = true;
                gridViewReturn.OptionsView.ShowFooter = true;
            }
        }

        private void btImport_Click(object sender, EventArgs e)
        {
            FileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = ToolConstants.Configuration.FilterFile.Excel;

            int rowCount = 0;

            INISettings iniSettings = new INISettings();
            string connectString = iniSettings.SQLConnectionString;
            OleDbConnection connection = new OleDbConnection();
            OleDbCommand command = null;

            // Create DbDataReader to Data Worksheet
            DbDataReader dr = null;

            // Bulk Copy to SQL Server 
            SqlBulkCopy bulkInsert = new SqlBulkCopy(connectString);

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {

                try
                {
                    string path = fileDialog.FileName;

                    // Connection String to Excel Workbook
                    string excelConnectionString =
                        string.Format(
                            "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;Persist Security Info=False",
                            path);

                    connection.ConnectionString = excelConnectionString;
                    connection.Open();

                    command = new OleDbCommand("select  COUNT(*) from [Sheet1$]", connection);
                    rowCount = (int)command.ExecuteScalar();

                    command = new OleDbCommand("select * from [Sheet1$]", connection);
                    dr = command.ExecuteReader();

                    bulkInsert.DestinationTableName = ToolConstants.DatabaseName + ".[dbo].[" + ToolConstants.Entities.Borrow.Name + "]";

                    SqlBulkCopyColumnMapping date = new SqlBulkCopyColumnMapping(ToolConstants.Entities.Borrow.FieldName.Date, ToolConstants.Entities.Borrow.FieldName.Date);
                    bulkInsert.ColumnMappings.Add(date);

                    SqlBulkCopyColumnMapping toolId = new SqlBulkCopyColumnMapping(ToolConstants.Entities.Borrow.FieldName.ToolID, ToolConstants.Entities.Borrow.FieldName.ToolID);
                    bulkInsert.ColumnMappings.Add(toolId);

                    SqlBulkCopyColumnMapping machineId = new SqlBulkCopyColumnMapping(ToolConstants.Entities.Borrow.FieldName.MachineID, ToolConstants.Entities.Borrow.FieldName.MachineID);
                    bulkInsert.ColumnMappings.Add(machineId);

                    SqlBulkCopyColumnMapping staffId = new SqlBulkCopyColumnMapping(ToolConstants.Entities.Borrow.FieldName.StaffID, ToolConstants.Entities.Borrow.FieldName.StaffID);
                    bulkInsert.ColumnMappings.Add(staffId);

                    SqlBulkCopyColumnMapping quantity = new SqlBulkCopyColumnMapping(ToolConstants.Entities.Borrow.FieldName.Quantity, ToolConstants.Entities.Borrow.FieldName.Quantity);
                    bulkInsert.ColumnMappings.Add(quantity);

                    SqlBulkCopyColumnMapping note = new SqlBulkCopyColumnMapping(ToolConstants.Entities.Borrow.FieldName.Note, ToolConstants.Entities.Borrow.FieldName.Note);
                    bulkInsert.ColumnMappings.Add(note);

                    Cursor.Current = Cursors.WaitCursor;
                    bulkInsert.WriteToServer(dr);

                    //write Log
                    log.Table = LogConstant.Table.ToolCutting.Borrow;
                    log.Task = LogConstant.Task.Import;
                    log.Description = string.Format(LogConstant.Message.Import, rowCount.ToString(), "");
                    log.WriteLog();
                    Cursor.Current = Cursors.Default;

                    Utilities.CreateLogFile(ToolConstants.Configuration.LogFileName.Borrow, string.Format(ToolConstants.Message.ImportSuccessfullyLog, rowCount.ToString()));
                    DialogResult message = MessageBox.Show(string.Format(ToolConstants.Message.ImportSuccessfully, rowCount.ToString()),
                                                           ToolConstants.Form.MessageBox.Message,
                                                           MessageBoxButtons.OKCancel);
                    if (message == DialogResult.OK)
                    {
                        BindDatatoGrid();
                    }

                }
                catch (Exception exception)
                {
                    Utilities.CreateLogFile(ToolConstants.Configuration.LogFileName.Borrow, string.Format(ToolConstants.Message.ImportFailedLog, exception.Message));
                    MessageBox.Show(exception.Message);
                }
                finally
                {
                    if (connection != null && connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                }
            }
        }

        protected virtual void InitDateRangeMenu()
        {
            menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem(ToolConstants.Configuration.DateRangeTitle.Today, new EventHandler(OnItemClickToday), null, null, null, null));
            menu.Items.Add(new DXMenuItem(ToolConstants.Configuration.DateRangeTitle.SixMonths, new EventHandler(OnItemClick6Months), null, null, null, null));
            menu.Items.Add(new DXMenuItem(ToolConstants.Configuration.DateRangeTitle.OneYear, new EventHandler(OnItemClick1Year), null, null, null, null));
            menu.Items.Add(new DXMenuItem(ToolConstants.Configuration.DateRangeTitle.TwoYears, new EventHandler(OnItemClick2Years), null, null, null, null));
            menu.Items.Add(new DXMenuItem(ToolConstants.Configuration.DateRangeTitle.AllTime, new EventHandler(OnItemClickAllTime), null, null, null, null));

        }

        void OnItemClickToday(object sender, EventArgs e)
        {
            BindDatatoGrid(4);
            dropbtDateRange.Text = ToolConstants.Configuration.DateRangeTitle.Today;
        }

        void OnItemClick6Months(object sender, EventArgs e)
        {
            BindDatatoGrid(1);
            dropbtDateRange.Text = ToolConstants.Configuration.DateRangeTitle.SixMonths;
        }

        void OnItemClick1Year(object sender, EventArgs e)
        {
            BindDatatoGrid(2);
            dropbtDateRange.Text = ToolConstants.Configuration.DateRangeTitle.OneYear;
        }

        void OnItemClick2Years(object sender, EventArgs e)
        {
            BindDatatoGrid(3);
            dropbtDateRange.Text = ToolConstants.Configuration.DateRangeTitle.TwoYears;
        }

        void OnItemClickAllTime(object sender, EventArgs e)
        {
            BindDatatoGrid(0);
            dropbtDateRange.Text = ToolConstants.Configuration.DateRangeTitle.AllTime;
        }

        private void ShowHideLink()
        {
            Permission role = new Permission();
            role.Is_Login();
            if (role.HasNotRight(PermissionConstants.Functions.Basic.Edit))
            {
                btEdit.Enabled = false;
                editToolStripMenuItem.Enabled = false;
                this.isDoubleClick = false;
            }

            if (role.HasNotRight(PermissionConstants.Functions.Basic.Delete))
            {
                btDelete.Enabled = false;
                deleteToolStripMenuItem.Enabled = false;
            }

        }
    }
}
